"""
@File: 版本5.py
@Author: orange
@time：2022/7/12 20:59
"""
import random

class Role():
    def __init__(self,name,blood=100):
        self.name = name
        self.blood = blood
        self.room = None
        self.equipment = None

    def attack(self,other):
        if self.room == other.room: # 在同一房间
            if self != other:
                if self.blood > 0 and other.blood > 0:
                    reduce_blood = 5 if other.equipment else 10
                    # 有装备就减5滴血，没有装备就减10滴血
                    other.blood -= reduce_blood
                    print(
                        f"{self.name}攻击{other.name} {reduce_blood}点血量\t{self.name}当前血量:{self.blood}\t{other.name}当前血量: {other.blood}")
                else:
                    print("其中一方已死亡，不能攻击")
                    exit()
            else:
                print("你不能攻击你自己")
        else:
            print("攻击对象不再同一个房间")

class Room():
    # 定义房间里最多人数
    MAX = 2
    def __init__(self, name):
        self.member = []
        self.name = name
    def join(self,role):
        if role.room == None:
            if len(self.member) < Room.MAX:
                self.member.append(role)
                role.room = self.name
            else:
                print("此房间已满")
        else:
            print(f"{role.name}角色已经加入了{role.room}房间了")

# 存放初始化实例
roles = {}
rooms = {}

while True:
    choice = input("请选择您的操作：\n1.创建角色\n2.创建房间\n3.加入房间\n4.开始战斗\n5.退出\n")
    if choice.isdigit():
        # 创建角色
        if choice == '1':
            name = input("请输入您的游戏名: ")
            roles[name] = Role(name)
            equip = input(f"是否为角色{name}装备防弹衣(y)\n")
            if equip.lower() == 'y':
                roles[name].equipment = 1
                print("装备成功")
            else:
                print("装备失败")
        # 创建房间
        elif choice == '2':
            name = input("请输入房间的名字\n")
            rooms[name] = Room(name)
        # 加入房间
        elif choice == '3':
            for i in roles.keys():
                print(i)
            print("&"*30)
            print("当前创建的房间\n")
            for i in rooms.keys():
                print(i)
            print("&" * 30)
            role_name, room_name = input("请输入要加入的角色和房间名，空格隔开\n").split()
            rooms[room_name].join(roles[role_name])
        # 开始战斗
        elif choice == '4':
            print(f"当前房间有{rooms.keys()}")
            room_name = input("请输入房间的名字\n")  # 注：选择房间
            if len(rooms[room_name].member) == 2:
                while True:
                    # 随机分配先手攻击的选手
                    res = random.randint(0,3)
                    if res % 2 == 1:
                        rooms[room_name].member[0].attack(rooms[room_name].member[1])
                        rooms[room_name].member[1].attack(rooms[room_name].member[0])
                    else:
                        rooms[room_name].member[1].attack(rooms[room_name].member[0])
                        rooms[room_name].member[0].attack(rooms[room_name].member[1])
            else:
                print(f"房间{room_name}人数不足2人，对战失败！")
        elif choice == '5':
            print("退出对战")
            break
    else:
        print("输入不规范，请重新输入")














